/*
 * @Author: 轻语
 * @Date: 2021-12-29 17:39:28
 * @LastEditors: 轻语
 * @LastEditTime: 2021-12-30 19:05:28
 * @Description: axios封装
 */
import { merge } from '@/utils/index'
import axios from 'axios';
// import { getTokenAUTH } from '@/utils/auth';
const TOKEN = '212121212132434'
const DEFAULT_AXIOS_CONFIG = {
  withCredentials: true, // 允许把cookie传递到后台
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json; charset=utf-8'
  },
  timeout: 15000,
}
// export default {
//   get: () => ({ }),
//   post: () => ({})
// }
// const contentTypes = {
//   json: 'application/json; charset=utf-8',
//   urlencoded: 'application/x-www-form-urlencoded; charset=utf-8',
//   multipart: 'multipart/form-data',
// }

// const defaultOptions = {
//   withCredentials: true, // 允许把cookie传递到后台
//   headers: {
//     Accept: 'application/json',
//     'Content-Type': contentTypes.json,
//   },
//   timeout: 15000,
// }


// export const fetch = ({
//   url,
//   data = {},
//   method = 'get',
//   options = {
//   },
//   contentType = 'json', // json || urlencoded || multipart
//   prefixUrl = 'api',
// }) => {
//   const fullUrl = `/${prefixUrl}/${url}`
  


//   const newOptions = merge({}, defaultOptions, options, { method }, data, {
//     headers: {
//       'Content-Type': options.headers && options.headers['Content-Type'] || contentTypes[contentType],
//     }
//   })
//   console.log(newOptions, 'newOptions')



const fetch = (url, {
  method = 'get',
  params = {},
  httpConfig = {},
  prefixUrl = 'api',
  contentType = ''
}) => {
  const axiosConfigIn = merge(
    DEFAULT_AXIOS_CONFIG,
    httpConfig
  )
  instance.interceptors.request.use(config => {
    config.headers.tokenId = TOKEN
    switch (method) {
      case 'get':
        config.params = params
        if (contentType) { // 未设置requestData的时候源码会删掉Content-Type的设置，其这么做的原因在于是Get请求本身是不需要Content-Type的。
          config.data = true
        }
        break;
      case 'post':
        
        break;
      default:
        config.data = params
        break;
    }
    console.log(config)
    return config
  })
  return instance({
    url: `/${prefixUrl}/${url}`,
    method,
    ...axiosConfigIn,
    ...(contentType && { headers: { 'content-type': contentType } })
  })
}

export const get = (url, arg) => fetch(url, { ...arg, method: 'get' })
export const post = (url, arg) => fetch(url, { ...arg, method: 'post' })